package com.sparkStreaming.demo

import org.apache.spark.SparkConf
import org.apache.spark.streaming.kafka.KafkaUtils
import org.apache.spark.streaming.{Seconds, StreamingContext}

/**
  * Spark Streaming对接Kafka的方式一
  * 不建议使用
  */
object _41_KafkaReceiverWordCount {

  def main(args: Array[String]): Unit = {

   if (args.length != 4) {
      System.err.println("Usage: KafkaReceiverWordCount <zkQuorum> <group> <topics> <numThreads>")
    }

    val Array(zkQuorum, group, topics, numThreads) = args

    val topicMap = topics.split(",").map((_, numThreads.toInt)).toMap

    val sparkConf = new SparkConf()
      .setAppName("KafkaReceiverWordCount")
      .setMaster("local[2]")

    val ssc = new StreamingContext(sparkConf, Seconds(5))



    //Spark Streaming对接Kafka
    val messages = KafkaUtils.createStream(ssc, zkQuorum, group, topicMap)
    //val messages = KafkaUtils.createStream(ssc, "hadoop000:2181", "test", "kafka_streaming_topic")

    //为什么要取第二个
    messages.map(_._2).flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _).print()

    ssc.start()
    ssc.awaitTermination()
  }
}
